home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / t3_1 / doc.lha / documentation / manual / state.mss < prev    next >
Text File  |  1987-06-30  |  11KB  |  313 lines

  1. @part[STATE, root "TMAN.MSS"]   @Comment{-*-System:TMAN-*-}
  2. @chap[Side effects]
  3.  
  4.  
  5. A @iixs[side-effect] is a change in the @ix[state] of a running @tau[]
  6. system, such as a change in the value stored in some location, or an
  7. effect on the outside world.
  8. @index[Locations]
  9.  
  10. Many useful programs can be written which make no use of side-effects.
  11. In principle, side-effects other than those controlling the outside world are
  12. unnecessary.  However, side effects, when used carefully, can be used to
  13. promote program modularity and readability.  In addition, they may be
  14. necessary in a @Tau[] implementation to obtain efficient program
  15. execution.
  16.  
  17.  
  18. @section[Assignment]
  19. @label[Assignmentsection]       @Comment{ref: number chapter, list chapter}
  20.  
  21. @tau[] provides several special forms which perform assignment to
  22. @i[locations].  A location is a place in which a value may be stored,
  23. for example, a variable, or the car of a list, or a component of a
  24. structure.  Locations are not objects @i[per se], although
  25. references to them may be obtained through the use of @termi[locatives].
  26.  
  27. @dc{ Talk about the syntax of @i[location] in the following. }
  28.  
  29. @AnEquivE(Tfn="SET",Efn="SETQ")
  30. @AnEquivE(Tfn="SET",Efn=":=")
  31. @info[NOTES="Special form",EQUIV="SETF"]
  32. @desc[(SET @i[location new-value]) @yl[] @i[new-value]]
  33. @tc[SET] is a generalized assignment operator.
  34. It alters the location specified by @i[location] to hold @i[new-value].
  35. The value of any @tc[SET]-expression is the @i[new-value] thus stored.
  36.  
  37. If @i[location] is a symbol, then it names a variable which has been
  38. previously bound by a @tc[LAMBDA] or an @tc[LSET] (or indirectly by
  39. any form which does @tc[LAMBDA]-binding, such as @tc[LET]),
  40. whose value is to be altered.
  41.   @begin[ProgramExample]
  42. (SET X (LIST 1 2 3))  @ev[]  (1 2 3)
  43. X                     @ev[]  (1 2 3)
  44.   @end[ProgramExample]
  45. @i[Location] may also have the syntax of a call to an @i[access-type]
  46. procedure or operation such as @tc[CAR].  (In this manual, these are
  47. marked as @i[Settable].)
  48.   @label[Settable]      @Comment{ref: operations chapter}
  49.   @begin[ProgramExample, LongLines keep]
  50. (SET (CAR X) 'A)  @ev[]  A
  51. X                 @ev[]  (A 2 3)
  52.  
  53. (SET (@i[proc arg@-[1]] ... @i[arg@-[n]]) @i[value])  @~
  54.   @ce[]  ((SETTER @i[proc]) @i[arg@-[1]] ... @i[arg@-[n] value])
  55.   @end[ProgramExample]
  56. @EndDesc[SET]
  57.  
  58. @info[NOTES="Operation"]
  59. @desc[(SETTER @i[procedure]) @yl[] @i[procedure]]
  60. Given an access routine, @tc[SETTER] returns a corresponding alteration
  61. routine.  @wt[(SETTER CAR)], for example, evaluates to a primitive
  62. procedure that takes two arguments, a list whose car is to be changed,
  63. and a value to store in the car of the list.
  64.  
  65. See also @tc[DEFINE-SETTABLE-OPERATION], page
  66. @pageref[DEFINE-SETTABLE-OPERATION].  @dc{ ? }
  67. @EndDesc[SETTER]
  68.  
  69. @info[NOTES="Special form"]
  70. @desc[(SWAP @i[location new-value]) @yl[] @i[object]]
  71. This behaves the same as @tc[SET], except that the value
  72. returned is the old value found in @i[location],
  73. rather than the @i[new-value].  For example,
  74. the following expression exchanges the values of the variables @tc[X] and
  75. @tc[Y]:
  76. @begin[ProgramExample]
  77. (SET X (SWAP Y X))
  78. @end[ProgramExample]
  79. @EndDesc[SWAP]
  80.  
  81. @info[NOTES="Special form"]
  82. @desc[(EXCHANGE @i[location1 location2]) @yl[] @i[undefined]]
  83. Exchanges the values in the two locations.
  84. @dc{Explain more fully.}
  85. @EndDesc[EXCHANGE]
  86.  
  87. @info[NOTES="Special form"]
  88. @desc[(MODIFY @i[location procedure]) @yl[] @i[object]]
  89. Stores a value in the @i[location] which is obtained by applying @i[procedure]
  90. to the old value in @i[location].
  91.   @begin[ProgramExample]
  92. (MODIFY @i[location] 1+)  @ce[]  (INCREMENT @i[location])
  93.   @end[ProgramExample]
  94. @enddesc[MODIFY]
  95.  
  96. @info[NOTES="Special form"]
  97. @desc[(MODIFY-LOCATION @i[location continuation]) @yl[] @i[object]]
  98. Calls @i[continuation], which should be a procedure of two arguments,
  99. passing it an access procedure
  100. of no arguments, which fetches the value in @i[location];
  101. and an update procedure of one argument, which will store a
  102. new value in @i[location].
  103. @dc{bletch}
  104.  
  105. @tc[MODIFY-LOCATION]'s purpose is to ensure that any subforms
  106. of the form for @i[location] are evaluated only once.  For example,
  107.   @begin[ProgramExample]
  108. (INCREMENT (CAR (HACK))
  109.   @ce[]
  110. (MODIFY-LOCATION (CAR (HACK))
  111.                  (LAMBDA (FETCH STORE) (STORE (+ (FETCH) 1))))
  112.   @end[ProgramExample]
  113. In this expression, @tc[(HACK)] will be evaluated only once, whereas in the following
  114. naive expansion
  115. @begin[ProgramExample]
  116. (SET (CAR (HACK)) (+ (CAR (HACK)) 1))
  117. @end[ProgramExample]
  118. it is evaluated twice, which is presumably neither necessary nor desirable.
  119. The expansion of @tc[MODIFY-LOCATION] forms introduces extra temporary
  120. variables, as needed, to account for this.
  121.  
  122. Although awkward when written directly,
  123. @tc[MODIFY-LOCATION] is useful in macro expansions.
  124. @tc[MODIFY-LOCATION] is the common special form in terms of which other
  125. @qu[modification] forms such as @tc[PUSH], @tc[MODIFY], and @tc[SWAP]
  126. are implementeded as macros.
  127.  
  128. For example, one might define a @tc[DOUBLE] macro, which multiplies by two the
  129. value in a location, as follows:
  130. @begin[ProgramExample]
  131. (DEFINE-SYNTAX (DOUBLE FORM)
  132.   `(MODIFY-LOCATION
  133.      ,FORM
  134.      (LAMBDA (FETCH STORE) (STORE (* (FETCH) 2)))))
  135.  
  136. (DOUBLE (CAR (HACK)))
  137. @end[ProgramExample]
  138. @EndDesc[MODIFY-LOCATION]
  139.  
  140.  
  141. @section[Locatives]
  142.  
  143. @label[locatives section]       @Comment{ref: environments chapter.}
  144.  
  145. @i[Locatives] in @tau[] are similar to the @i[pointers] or
  146. @i[references] of languages like C or Algol 68.
  147.  
  148. Locatives are obtained by evaluating @tc[LOCATIVE]-expressions.  The
  149. value in the location they refer to may be fetched using the
  150. @tc[CONTENTS] operation, and stored using @tc[SET].
  151.  
  152. Objects which act as locatives may be created using @tc[OBJECT], as long
  153. as they handle the @tc[CONTENTS] and @tc[(SETTER CONTENTS)] operations
  154. appropriately, and handle @tc[LOCATIVE?] by returning true.
  155.  
  156.  
  157. @AnEquivE[Tfn="LOCATIVE",Efn="LOCF"]
  158. @info[NOTES="Special form"]
  159. @desc[(LOCATIVE @i[location]) @yl[] @i[locative]]
  160. Returns a locative which accesses the location specified by @i[location].
  161. @i[Location] receives similar treatment to the @i[location] position
  162. in the @tc[SET] special form; it may refer to a variable, or to a
  163. @qu"field" within some mutable structure, for example the car of a pair.
  164.  
  165. The following equivalence approximates the behavior of @tc[LOCATIVE]:
  166.   @begin[ProgramExample]
  167. (LOCATIVE @i[location])
  168.   @ce[]
  169. (OBJECT NIL
  170.         ((CONTENTS SELF) @i[location])
  171.         (((SETTER CONTENTS) SELF VALUE) (SET @i[location] VALUE))
  172.         ((LOCATIVE? SELF) T))
  173.   @end[ProgramExample]
  174. This is accurate if @i[location] is a variable, but if it is a call,
  175. the subforms of the call are evaluated when the @tc[LOCATIVE]-expression
  176. is evaluated, not when the contents of the locative are required.
  177. For example,
  178.   @begin[ProgramExample]
  179. (LOCATIVE (FOO (BAR BAZ)))
  180.   @ce[]
  181. (LET ((TEMP1 FOO)
  182.       (TEMP2 (BAR BAZ)))
  183.   (OBJECT NIL
  184.           ((CONTENTS SELF) (TEMP1 TEMP2))
  185.           (((SETTER CONTENTS) SELF VALUE) (SET (TEMP1 TEMP2) VALUE))
  186.           ((LOCATIVE? SELF) T)))
  187.   @end[ProgramExample]
  188. @EndDesc[LOCATIVE]
  189.  
  190. @info[NOTES="Settable operation"]
  191. @desc[(CONTENTS @i[locative]) @yl[] @i[object]]
  192. Dereferences (indirects through) the @i[locative].
  193.   @begin[ProgramExample]
  194. (LSET Z (LIST 'A 'B))                   @ev[]  (A B)
  195. (CONTENTS (LOCATIVE (CAR Z)))           @ev[]  A
  196. (SET (CONTENTS (LOCATIVE (CAR Z))) 'C)  @ev[]  C
  197. Z                                       @ev[]  (C B)
  198.  
  199. (CONTENTS (LOCATIVE @i[location]))  @ce[]  @i[location]
  200. (SET (CONTENTS (LOCATIVE @i[location])) @i[value]  @ce[]  (SET @i[location] @i[value])
  201.   @end[ProgramExample]
  202. @EndDesc[CONTENTS]
  203.  
  204. @info[NOTES="Type predicate operation"]
  205. @desc[(LOCATIVE? @i[object]) @yl[] @i[boolean]]
  206. Returns true if @i[object] is a locative.
  207. @EndDesc[LOCATIVE?]
  208.  
  209.  
  210. @section[Dynamic state]
  211. @label[dynamic state section]   @Comment{ref: control chapter.}
  212. @index[dynamic state]
  213.  
  214. @AnEquivE[Tfn="BIND",Efn="SPECIAL"]
  215. @AnEquivE[Tfn="BIND",Efn="LET"]
  216. @info[NOTES="Special form"]
  217. @desc[(BIND @i[specs] . @i[body]) @yl[] @i[value-of-body]]
  218.  
  219. @tc[BIND] implements @ix[dynamic binding].  Syntactically, it is similar
  220. to @tc[LET], but semantically it is completely different.  @tc[BIND]
  221. operates by doing temporary assignments to locations, for example, to
  222. bound variables.  Unlike @tc[LET], it does not introduce a new lexical
  223. binding contour.
  224.  
  225. Note that the use of the term @i[bind] in this context is
  226. unrelated to its use elsewhere (for example, at the beginning of chapter
  227. @ref[environments]).
  228.  
  229. Each @i[spec] should have the syntax
  230.   @begin[ProgramExample]
  231. (@i[location] @i[value])
  232.   @end[ProgramExample]
  233. The value in each @i[location] is obtained and saved.
  234. Each @i[location] is assigned the @i[value], as with @tc[SET].
  235. The @i[body] expressions are evaluated and the value of the last
  236. is saved.  The @i[locations] are then restored to their original
  237. saved values.  The @tc[BIND]-expression finally yields the saved value
  238. of the @i[body].
  239.   @begin[ProgramExample]
  240. (BIND ((@i[location] @i[value])) . @i[body])
  241.   @end[ProgramExample]
  242. is therefore similar to
  243.   @begin[ProgramExample]
  244. (LET ((SAVED-VALUE @i[location]))
  245.   (SET @i[location] @i[value])
  246.   (BLOCK0 (BLOCK . @i[body])
  247.           (SET @i[location] SAVED-VALUE)))
  248.   @end[ProgramExample]
  249. or
  250.   @begin[ProgramExample]
  251. (LET ((SAVED-VALUE @i[location]))
  252.   (SET @i[location] @i[value])
  253.   (UNWIND-PROTECT (BLOCK . @i[body])
  254.                   (SET @i[location] SAVED-VALUE)))
  255.   @end[ProgramExample]
  256. Examples:
  257.   @begin[ProgramExample]
  258. (LSET A 1)
  259. (DEFINE (F) (+ A 1))
  260. (F)                   @ev[]  2
  261. (LET  ((A 10)) (F))   @ev[]  2
  262. (BIND ((A 10)) (F))   @ev[]  11
  263.  
  264. (LSET X (LIST 1 2 3))
  265. (BIND (((CAR X) 10))
  266.   (LSET Y (COPY-LIST X)))
  267. X  @ev[]  (1 2 3)
  268. Y  @ev[]  (10 2 3)
  269.   @end[ProgramExample]
  270. The values in the @i[locations] are restored, even
  271. if a throw out of @i[body] occurs (see @tc[CATCH], page @pageref[CATCH]).
  272. For example:
  273.   @begin[TEG]
  274. (LET ((A 10))
  275.   (CATCH EP (BIND ((A 20)) (EP NIL)))
  276.   A)
  277.   @ev[]  10
  278.   @end[TEG]
  279. Any @i[location] in a @tc[BIND]-form which is a variable name
  280. should name a variable which has been previously bound
  281. using, for example, @tc[LSET] or @tc[LET].
  282. @EndDesc[BIND]
  283.  
  284. @info[NOTES="Special form"]
  285. @Desc[(UNWIND-PROTECT @i[form . unwind-forms]) @yl[] @i[value-of-form]]
  286. @tc[UNWIND-PROTECT] behaves nearly the same as @tc[BLOCK0]:
  287. the @i[form] is evaluated, and the value is saved; the @i[unwind-forms]
  288. are evaluated; and the @tc[UNWIND-PROTECT]-expression yields the (saved)
  289. value of @i[form].  However, unlike @tc[BLOCK0],
  290. @tc[UNWIND-PROTECT] guarantees that 
  291. the @i[unwind-forms] will be evaluated,
  292. even if a throw occurs
  293. during the evaluation of @i[form].
  294.  
  295. In the following example, the motor will be stopped even if @tc[DRILL-HOLE]
  296. attempts to throw out of the @tc[UNWIND-PROTECT].
  297.   @begin[ProgramExample]
  298. (UNWIND-PROTECT (BLOCK (START-MOTOR)
  299.                        (DRILL-HOLE))
  300.                 (STOP-MOTOR))
  301.   @end[ProgramExample] 
  302.  
  303. A throw (see page @pageref[CATCH]) out of the @tc[BLOCK] @dash[] for example,
  304. as a result of calling @tc[DRILL-HOLE], will evaluate the exit form
  305. @wt[(STOP-MOTOR)] before control finally returns to its corresponding
  306. @tc[CATCH].
  307.  
  308. It is an error to throw out of the unwind forms of an @tc[UNWIND-PROTECT].
  309. @EndDesc[UNWIND-PROTECT]
  310.  
  311. @dc{ Talk about dynamic state; @qu"soft" and @qu"hard" throws;
  312. the difference between @tc[BIND] and @tc[UNWIND-PROTECT]; etc. }
  313.